package com.fnfn.sdk.api.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by huangfei on 2018/10/11.
 */
public class BaseService {

    public static void main(String[] args) {
        System.out.println("2018-10-31 18:10:23.952".substring(0, 19));
    }

    protected Logger logger = LoggerFactory.getLogger(getClass());

    protected String value(Object value) {
        if (value == null) {
            return null;
        }

        String s = value.toString();
        if (s.contains("-") && s.contains(":")) {
            if (s.length() > 19) {
                return s.substring(0, 19);
            }
        }

        return s;
    }

    protected int valueInt(Object value) {
        if (value == null) {
            return 0;
        }
        return Integer.valueOf(value.toString());
    }

    /**
     * 日期转时间戳  edit by liyang
     */
    public static long dateToTimestamp(String datetime) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        long timestamp = 0;
        try {
            Date date = sdr.parse(datetime);
            timestamp = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timestamp;
    }

    /**
     * 时间戳转日期 edit by liyang
     */
    public static Date timestampToDate(String datetime) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        long timestamp = Long.valueOf(datetime);
        Date date = new Date(timestamp);
        return date;
    }

}
